package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.HorarioDAORemote;
import proyecto.EntityBeans.Horario;

@Stateless
@Remote(HorarioBLRemote.class)
public class HorarioBL implements HorarioBLRemote {

	HorarioDAORemote horarioDAORemote;
	
	public int save(Horario horario) {	
		
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return horarioDAORemote.save(horario); //alta de horario		
		
	}
		
	public Horario findById(int id) {
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return horarioDAORemote.findById(id);
		
	}
	
		
	public void merge(Horario horario) {
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
			horarioDAORemote.merge(horario);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	

	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return horarioDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return horarioDAORemote.findByIdList(id);
	}
	
	
	public List findByIdGrupo(int idGrupo)
	{	     
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return horarioDAORemote.findByIdGrupo(idGrupo);
	}
	
		
	public void remove(int idHorario){	
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		horarioDAORemote.remove(idHorario);
	}
	
	
		
	public void refresh(Horario horario){
		try
		{
			Context context = new InitialContext();
			horarioDAORemote = (HorarioDAORemote) context.lookup("HorarioDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		horarioDAORemote.refresh(horario);
	}
}
